package br.com.JogoMemo;

import java.awt.print.Printable;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class GameEngine
 */
@WebServlet("/GameEngine")
public class GameEngine extends HttpServlet {
	private static final long serialVersionUID = 1L;

	private ArrayList<Integer> listNumber = new ArrayList<Integer>();
	private ArrayList<Integer> guessNumber = new ArrayList<Integer>();

	public GameEngine() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		doScenario(request, response);
	}

	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		doGame(request, response);
		// doScenario(request, response);
	}

	protected void doGame(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		int guess = Integer.parseInt(request.getParameter("numero"));
		int played = Integer.parseInt(request.getParameter("played")+1);
		int playCount;
	
		PrintWriter out = response.getWriter();
        
		
		guessNumber.add(guess);
        
		
		for (Integer o : guessNumber) {
			if (listNumber.contains(o)) {
				out.println("Correto!");
				break;
			}

			else {
//				listNumber.removeAll(listNumber);
//				guessNumber.removeAll(guessNumber);
                guessNumber.clear();
				out.println("Errado!");
			}
		}

	}// fim doGame
	
	protected void doScenario(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		
		int newNumber = Integer.parseInt(request.getParameter("start"));
		GameEngine game = new GameEngine();
		PrintWriter out = response.getWriter();
		
		if (newNumber == 1) {
			
			listNumber.add(game.doRandom());
			out.println(listNumber);
			
//			for (Integer str : listNumber) {
//				//out.println(str);
//			}
			
		} else {

		}
		

	}
	

	protected int doRandom() throws ServletException, IOException {

		int i = (int) (1 + Math.random() * 1000);

		return i;
	}

}
